<?php 
namespace app\api\controller;

use think\Exception;
use think\Controller;
use app\common\exception\BaseException;
use app\api\model\Article as ArticleModel;
use app\api\model\Ip as IpModel;
class Article extends Controller
{
    public function getList()
    {
        $article = new ArticleModel();
        if($this->request->param("id")) {
            $data = $article->where("id", $this->request->param("id"))->find();
            // 做个连表查询
            $count = IpModel::where('article_id', $this->request->param("id"))->select()->count();
            $data->count = $count;
            return json([
                "msg"=>"查找成功",
                "code"=>20000,
                "data"=> $data
            ],200);
        } else if($this->request->param("project_id")) {
            $data = $article->where('project_id', $this->request->param("project_id"))->page($this->request->param("page"),$this->request->param("pageSize"))->select();
            $count =  $article->where('project_id', $this->request->param("project_id"))->order('id', 'desc')->select()->count();
            return json([
                "msg"=> "查找成功",
                "code"=> 20000,
                "data"=> $data,
                "page"=> $this->request->param("page"),
                "pageSize"=> $this->request->param("pageSize"),
                "pageTotal"=> $count
            ],200);
        }else {
            if($this->request->param("page") && $this->request->param("pageSize")) {
                if($this->request->param("title")) {
                    $data = $article->where("title", 'like', '%'.$this->request->param("title").'%')->page($this->request->param("page"),$this->request->param("pageSize"))->order('id', 'desc')->select();
                    $count = $article->where("title", 'like', '%'.$this->request->param("title").'%')->order('id', 'desc')->select()->count();
                } else if($this->request->param("type")) {
                    $data = $article->where("type", 'like', '%'.$this->request->param("type").'%')->page($this->request->param("page"),$this->request->param("pageSize"))->order('id', 'desc')->select();
                    $count = $article->where("type", 'like', '%'.$this->request->param("type").'%')->order('id', 'desc')->select()->count();
                } else {
                    $data = $article->page($this->request->param("page"),$this->request->param("pageSize"))->order('id', 'desc')->select();
                    $count = $article->order('id', 'desc')->select()->count();  
                }
                return json([
                    "msg"=> "查找成功",
                    "code"=> 20000,
                    "data"=> $data,
                    "page"=> $this->request->param("page"),
                    "pageSize"=> $this->request->param("pageSize"),
                    "pageTotal"=> $count
                ],200);
            }else {
                throw new BaseException('分页信息不能为空');
            }
        }
        dump($data);
    }

    public function addArticle() {
        if($this->request->param('id')) {
            $article = ArticleModel::get($this->request->param('id'));
            $article->title = $this->request->param('title');
            $article->type = $this->request->param('type');
            $article->project_id = $this->request->param('project_id');
            $article->content =  $this->request->param('content');
            $article->subtitle =  $this->request->param('subtitle');
            $article->introduction =  $this->request->param('introduction');
            $article->update_time = time();
            $result = $article->save();
            if($result) {
                return json([
                    'msg'=> '修改成功',
                    'code'=> 20000,
                ],200);
            }else {
                throw new Exception();
            }
        }else {
            $data = $this->request->param();
            $data["create_time"] = time();
            $data["update_time"] = time();
            $article = new ArticleModel();
            $result = $article->save($data);
            if($result) {
                return json([
                    'msg'=> '创建成功',
                    'code'=> 20000,
                ],200);
            }else {
                throw new Exception();
            }
        }

    }

    public function deleteArticle() {
        $article = ArticleModel::get($this->request->param('id'));
        $result = $article->delete();
        if($result) {
            return json([
                'msg'=> '删除成功',
                'code'=> 20000,
            ],200);
        }else {
            throw new Exception();
        }
    }

    public function getTypeList() {
        $article = new ArticleModel();
        if ($this->request->param("page")) {
            $data = $article->where("title|type", 'like', '%'.$this->request->param("title").'%')
            ->page($this->request->param("page"),$this->request->param("pageSize"))
            ->order('id', 'desc')
            ->select();
            $count =  $article->where("title|type", 'like', '%'.$this->request->param("title").'%')->select();
        }else {
            $data = $article->where("title|type", 'like', '%'.$this->request->param("title").'%')
            ->order('id', 'desc')
            ->select();
            $count = $data->count();
        }
        
        return json([
            "msg"=> "查找成功",
            "code"=> 20000,
            "data"=> $data,
            "page"=> $this->request->param("page"),
            "pageSize"=> $this->request->param("pageSize"),
            "pageTotal"=> $count
        ],200);
    }

}